package com.aygxy.carproject.service.impl;

import com.aygxy.carproject.ex.ServiceCode;
import com.aygxy.carproject.ex.ServiceException;
import com.aygxy.carproject.mapper.MealMapper;
import com.aygxy.carproject.pojo.meal.dto.MealAddNewDTO;
import com.aygxy.carproject.pojo.meal.dto.MealUpdateDTO;
import com.aygxy.carproject.pojo.meal.eneity.Meal;
import com.aygxy.carproject.pojo.meal.vo.MealStandardVO;
import com.aygxy.carproject.pojo.product.entity.Sku;
import com.aygxy.carproject.pojo.product.vo.SkuStandardVO;
import com.aygxy.carproject.service.MealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 套餐业务实现类
 */
@Service
@Slf4j
public class MealServiceImpl implements MealService {

    @Autowired
    private MealMapper mealMapper;

    @Override
    public MealStandardVO getById(Long id) {
        MealStandardVO meal = mealMapper.getById(id);
        if (meal == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "获取套餐信息失败，尝试访问的数据不存在！");
        }
        return meal;
    }

    @Override
    public void addNew(MealAddNewDTO mealAddNewDTO) {

    }

    @Override
    public void updateById(Long id, MealUpdateDTO mealUpdateDTO) {
        MealStandardVO MealQueryResult = mealMapper.getById(id);
        if (MealQueryResult == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "更新套餐失败，尝试访问的数据不存在！");
        }

        Meal meal = new Meal();
        BeanUtils.copyProperties(mealUpdateDTO, meal);
        int rows = mealMapper.updateById(meal);
        if (rows != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "更新套餐失败，服务器忙，请稍后再次尝试！");
        }
    }

    @Override
    public List<MealStandardVO> selectAllList() {
        return mealMapper.selectAllList();
    }
}
